package com.wiretrack.circuit;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;

import org.dom4j.DocumentException;

public class MyGui extends JPanel
{
	private static final long serialVersionUID = 1L;
	private static final String INTRO = "intro";
	private static final String USED_BEFORE = "used before";
	static Configuration configuration;
	private CardLayout cardLayout = new CardLayout();
	private JLabel countDownLabel = new JLabel("", SwingConstants.CENTER);
	private JLabel messageLabel = new JLabel("", SwingConstants.CENTER);
	private JLabel introLabel = new JLabel("Hello", SwingConstants.CENTER);
	JPanel introPanel = new JPanel();
	
	public MyGui()
	{
		JPanel introSouthPanel = new JPanel();
		introSouthPanel.add(new JLabel("Status:"));
		introSouthPanel.add(countDownLabel);
		introSouthPanel.add(messageLabel);

		
		introPanel.setPreferredSize(new Dimension(400, 300));
		introPanel.setLayout(new BorderLayout());
		introPanel.add(introLabel, BorderLayout.CENTER);
		introPanel.add(introSouthPanel, BorderLayout.SOUTH);

		JPanel usedBeforePanel = new JPanel(new BorderLayout());
		usedBeforePanel.setBackground(Color.pink);
		usedBeforePanel.add(new JLabel("Used Before", SwingConstants.CENTER));

		setLayout(cardLayout);
		add(introPanel, INTRO);
		add(usedBeforePanel, USED_BEFORE);

		new HurdlerTimer(this).start();
	}

	private static void createAndShowUI()
	{
		JFrame frame = new JFrame("Welcome");
		frame.getContentPane().add(new MyGui());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setLocationRelativeTo(null);
		// System.out.println("Using config: " + configuration.toString());
		frame.setVisible(true);
	}

	public static void setConfiguration(Configuration config)
	{
		configuration = config;
	}

	public static void main(String[] args)
	{
		Configuration config = null;
		try
		{
			setConfiguration(config.getInstance("./src/test/resources/config1.xml"));
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
		catch (DocumentException e)
		{
			e.printStackTrace();
		}
		java.awt.EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				createAndShowUI();
			}
		});
	}

	public void setCountDownLabelText(String text)
	{
		countDownLabel.setText(text);
	}
	
	public void setIntroLabelText(String text)
	{
		introLabel.setText(text);
	}
	public void showDonePanel()
	{
		cardLayout.next(this);
	}
}

class HurdlerTimer
{
	private static final int TIMER_PERIOD = 1000;
	protected static final int MAX_COUNT = 20;
	private MyGui gui;
	private int count;
	private int totalNumberOfRounds;

	public HurdlerTimer(MyGui gui)
	{
		this.gui = gui;
		String text = "(" + (MAX_COUNT - count) + ") seconds left";
		gui.setCountDownLabelText(text);
		totalNumberOfRounds = gui.configuration.rounds.rounds.size();
	}

	public void start()
	{
		new Timer(TIMER_PERIOD, new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				count++;
				
				if ( totalNumberOfRounds <= 0 )
				{
					((Timer) e.getSource()).stop();
					gui.showDonePanel();
				}
				else if ( count < MAX_COUNT )
				{
//					gui.setCountDownLabelText(Integer.toString((MAX_COUNT - count)));
					gui.setIntroLabelText(Integer.toString((MAX_COUNT - count)));
				}
				else
				{
					count = 0;
					totalNumberOfRounds--;
					gui.setCountDownLabelText("else: " + (MAX_COUNT - count));
					gui.setIntroLabelText("here: " + totalNumberOfRounds);
					System.out.println("count: " + count + " totalNumber: " + totalNumberOfRounds);
				}
			}

			private void checkIfWarning(int currentCount)
			{
//				if ( MyGui.configuration.restwarning == currentCount )
//				{
//					playSound(MyGui.configuration.restwarningsound);
//				}
			}

			private void playSound(String sound)
			{
				System.out.println("TODO: play sound: " + sound);

			}
		}).start();
	}
}

class MyActionListener implements ActionListener
{
	int totalTime;

	public MyActionListener(int i)
	{
		totalTime = i;
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		System.out.println("here with " + totalTime);
	}

}
